package com.dhcc.sdc.busi.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.service.ICdcHospitalService;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sdc.busi.service.ISdcHomeIndexService;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.base.service.ISdsQcentityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("${sys.backendPath}/sdc/home")
public class SdcHomeIndexController {
    private String PREFIX = "/backend/sdc/home/";

    private boolean isRole = false;
    @Autowired
    private ISdcHomeIndexService sdcHomeIndexService;
    @Autowired
    private ICdcHospitalService cdcHospitalService;
    @Autowired
    private ISdsCommonService sdsCommonService;
    @Autowired
    private ISdsQcentityService sdsQcentityService;
    @RequestMapping("")
    public String home(@RequestParam Map<String, Object> map, Model model) {
        //获取部门信息
        JSONArray officMap = cdcHospitalService.getOfficeMap();
        //加载用户关联科室
        JSONArray departMap = sdsCommonService.getDepartMap(new HashMap<>());
        //获取病种
        JSONArray entityMap = sdsQcentityService.getUpEntityMap();

        model.addAttribute("officeMap", officMap);
        model.addAttribute("departMap", departMap);
        model.addAttribute("entityMap", entityMap);
        model.addAttribute("departCode", map.get("departCode"));
        model.addAttribute("departDesc", map.get("departDesc"));
        model.addAttribute("drgCode", map.get("drgCode"));
        model.addAttribute("hospitalId", sdsCommonService.getHospIdBySysUser());
        return  PREFIX + "home.html";
    }

    /**
     * @Description: 顶部和中部的数据查询
     * @Title: homeData
     */
    @RequestMapping("/homeData")
    @ResponseBody
    public Object homeData(@RequestParam Map<String, Object> map){
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        map.put("userLinkDepts", deptIds);
        return sdcHomeIndexService.getHomeData(map);
    }

    /**
     * @Description: 首页下面表格数据
     * @Title: selectAllDepartData
     */
    @RequestMapping("/selectAllDepartData")
    @ResponseBody
    public Object selectAllDepartData(@RequestParam Map<String, Object> map){
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        map.put("userLinkDepts", deptIds);
        return sdcHomeIndexService.selectAllDepartData(map);
    }

    /**
     * 首页点击图例中数字跳转页面
     * @param key
     * @param model
     * @return
     */
    @RequestMapping("/to_case")
    public String caseKey(@RequestParam Map<String, Object> map,Model model) {
        String key = map.get("key").toString();
        model.addAttribute("key", map.get("key"));
        model.addAttribute("office", map.get("office"));
        model.addAttribute("depart", map.get("depart"));
        model.addAttribute("doctor", map.get("doctor"));
        model.addAttribute("medicalGroup", map.get("medicalGroup"));
        model.addAttribute("drgType", map.get("drgType"));
        model.addAttribute("desc", map.get("desc"));
        if (CommonUtil.equals("drgNum", key)) {
            return "/backend/sdc/qcentity/" + "qcEntityDetail.html";
        } else {
            return "/backend/sdc/qccase/" + "qcCaseDetail.html";
        }
    }
    /**
     * 获取触发质控的患者数据
     * @param params
     * @return
     */
    @RequestMapping("/qcCaseList")
    @ResponseBody
    public Object qcCaseList(@RequestParam Map<String, Object> params) {
        Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        List<Map<String, Object>> result = sdcHomeIndexService.getQcCaseList(page, params);
        page.setRecords((List<Map<String, Object>>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 获取质控病种的数据
     * @param params
     * @return
     */
    @RequestMapping("/qcEntityList")
    @ResponseBody
    public Object qcEntityList(@RequestParam Map<String, Object> params) {
        Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        List<Map<String, Object>> result = sdcHomeIndexService.getQcEntityList(page, params);
        page.setRecords((List<Map<String, Object>>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }
}
